﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.DL.Data.Common;
using ScrumTable.DL.Data.Schema;
using ScrumTable.DL.Data.Schema.ScrumTableV1;

#endregion

namespace ScrumTable.DL.Data.Generic.Test.Mocks
{
    /// <summary>
    /// This class contains the facilities to test raised events
    /// from an data access object.
    /// </summary>
    public class DataAccessObjectEventTestStub
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly GenericDataContextBase _dataContext;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the number of raised and expected (target schema) events.
        /// </summary>
        public int ExpectedEventFireCount { get; private set; }

        /// <summary>
        /// Gets the number of raised events which do  not correspond to the target schema.
        /// </summary>
        public int OtherEventFireCount { get; private set; }

        /// <summary>
        /// Gets/sets element id of the changing element.
        /// </summary>
        public string ElementId { get; set; }

        /// <summary>
        /// Gets/sets the expected action on the data access object.
        /// </summary>
        public DataAccessObjectChangedAction Action { get; set; }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="DataAccessObjectEventTestStub"/> class.
        /// </summary>
        /// <param name="dataContext">Specifies the context which contains the data access objects.</param>
        internal DataAccessObjectEventTestStub(GenericDataContextBase dataContext)
        {
            PreCondition.AssertNotNull(dataContext, "dataContext");

            _dataContext = dataContext;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Registers the internal event listeners to the data access objects.
        /// </summary>
        /// <param name="testSchema">Specifies the target data access object schema.</param>
        public void RegisterEvents(SchemaBase testSchema)
        {
            _dataContext.Schema.Schemata.ForEach(
                schema =>
                {
                    if (schema == testSchema)
                    {
                        _dataContext.Storage[testSchema].DataAccessObjectChanged += OnExpectedEventRetrieved;
                    }
                    else
                    {
                        _dataContext.Storage[schema].DataAccessObjectChanged += OnOtherEventRetrieved;
                    }
                });

        }

        /// <summary>
        /// Unregisters all events registered by the RegisterEvents() procedure.
        /// </summary>
        public void UnregisterEvents()
        {
            _dataContext.Schema.Schemata.ForEach(schema => _dataContext.Storage[schema].DataAccessObjectChanged -= OnOtherEventRetrieved);
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        private void OnExpectedEventRetrieved(object sender, DataAccessObjectChangedEventArgs args)
        {
            if (args.Action == Action && args.ItemIdChain.Top == ElementId)
            {
                ++ExpectedEventFireCount;
            }
            else
            {
                --ExpectedEventFireCount;
            }
        }

        private void OnOtherEventRetrieved(object sender, DataAccessObjectChangedEventArgs args)
        {
            ++OtherEventFireCount;
        }

        #endregion
    }
}